package com.pdg.mcplugin.payd2mine;

import java.util.HashMap;
import java.util.Map;

import net.milkbowl.vault.economy.Economy;

import org.bukkit.entity.Player;

import com.pdg.mcplugin.common.baseclasses.PluginClientBase;

public class DataCache extends PluginClientBase<Payd2Mine> {

	private Map<String,Long> blockCache = new HashMap<String,Long>();
	private Map<String,Double> payCache = new HashMap<String,Double>();
	
	public DataCache(Payd2Mine thePlugin) {
		super(thePlugin);
	}
	
	public Long getValueFromBlockCache(String theKey){
		return blockCache.get(theKey);
	}
	
	public void addBlockToCache(String theKey, Long theValue){
		blockCache.put(theKey, theValue);
	}

	public void addPlayerPay(String name, double theAmount) {
		if(payCache.containsKey(name)){
			payCache.put(name, payCache.get(name)+theAmount);
		}else{
			payCache.put(name, theAmount);
		}
	}
	
	public void payPlayers(){
		Economy theEconomy = getPlugin().getEconomy();
        for (String thePlayerName : payCache.keySet())
        {
        	double theAmount = payCache.get(thePlayerName);
        	theEconomy.depositPlayer(thePlayerName, theAmount);

        	if(getPlugin().getMessageManager().isMessageEnabled(P2MMessages.ALERT)){
        		Player thePlayer = getPlugin().getServer().getPlayer(thePlayerName);
        		if(thePlayer!=null){
        			getPlugin().sendInformationalMessage(thePlayer, String.format(getPlugin().getMessageManager().getMessageText(P2MMessages.ALERT), theEconomy.format(theAmount)));
        		}
        	}
        }
        payCache.clear();
	}

	public void removeBlockFromCache(String theBlockInfo) {
		blockCache.remove(theBlockInfo);
	}

	public void clearCachedBlocks() {
		blockCache.clear();
	}

	public int getBlockCacheSize() {
		return blockCache.size();
	}

}
